<template>
  <div class="groupNumber">
    <el-form :model="results" :inline="true" size="mini" ref="ruleForm" label-width="100px">
      <div style="margin:20px">
        <el-form-item label="出团类型">
          <el-select v-model="results.countryType" placeholder="请选择">
            <el-option v-for="item in groupType" :key="item.label" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="团状态">
          <el-select v-model="results.groupStart" placeholder="请选择">
            <el-option v-for="item in options" :key="item.label" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="团名称">
          <el-input v-model="results.groupName"></el-input>
        </el-form-item>
        <el-form-item label="团号">
          <el-input v-model="results.groupNumber"></el-input>
        </el-form-item>
      </div>
      <div style="margin-top:20;margin-left: 45%;">
        <el-form-item>
          <el-button type="primary" @click="addproduct">新增</el-button>
          <el-button type="primary" @click="viewproduct">查询</el-button>
        </el-form-item>
      </div>
    </el-form>
    <!-- 表格数据 -->
    <el-table :data="tableData"
      :header-cell-style="{ color: '#fff', background: '#6090EC', fontSize: '11px', fontWeight: 'normal' }" size="mini"
      border>
      <!-- <el-table-column prop="countryTypeName" label="团类型" width="150">
        <template slot-scope="scope">
          <template v-if="scope.row.countryTypeName=='domesticGroup'">国内团</template>
          <template v-if="scope.row.countryTypeName=='foreignMissions'">国外团</template>
        </template>
      </el-table-column> -->
      <el-table-column prop="groupName" label="团名称" width="180">
      </el-table-column>
      <el-table-column prop="groupNumber" label="团号" width="140">
      </el-table-column>
      <el-table-column prop="roundTrip" label="往返时间"></el-table-column>
      <el-table-column prop="numberStatusName" label="团状态" width="90">
        <!-- <template slot-scope="scope">
          <template v-if="scope.row.numberStatusName=='0'">未上架</template>
          <template v-if="scope.row.numberStatusName=='1'">已上架</template>
        </template> -->
      </el-table-column>
      <el-table-column prop="orderCount" label="订单量" width="90"></el-table-column>
      <el-table-column prop="applicantsNum" label="已报人数" width="90"></el-table-column>
      <el-table-column prop="remainingPlaces" label="剩余名额" width="90"></el-table-column>
      <el-table-column label="操作" width="260">
        <template slot-scope="scope">
          <el-button slot="reference" type="primary" size="mini" @click="Mgroupodify(scope.row)">修改</el-button>
          <el-button v-if="scope.row.numberStatusName=='待上架'" slot="reference" type="primary" size="mini" @click="shelves(scope.row)">上架</el-button>
          <el-button  v-if="scope.row.numberStatusName=='已上架'" slot="reference" type="primary" size="mini" @click="shelves(scope.row)">下架</el-button>
          <el-button slot="reference" type="primary" size="mini" @click="viewgroup(scope.row)">查看团信息</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
      :page-sizes="[10, 15, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="pageTotal">
    </el-pagination>
    <!-- 新增/修改按钮 -->
    <el-dialog :visible.sync="dialogFormVisible" width="50%" :title="title" :close-on-click-modal="false">
      <el-form :inline="true" class="demo-form-inline" :model="addform" :rules="rules" ref="addform"
        style="border-bottom:1px solid #ccc">
        <el-form-item label="供应商:" prop="supplierId">
          <el-select size="mini" clearable placeholder="供应商" v-model="addform.supplierId" class="select"       @change="supplierIdChange" >
            <el-option v-for="(item,index) in travelSupplierVoList" :label="item.supplierName" :value="item.supplierId"
              :key="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合同号:" prop="contractId">
          <el-select v-model="addform.contractId" size="mini" placeholder="请选择"  @change="contractIdChange">
            <el-option v-for="item in tractNumList" :key="item.contractId" :label="item.contractNum"
              :value="item.contractId">
            </el-option>
          </el-select>
        </el-form-item><br />
        <el-form-item label="团名称:" prop="groupId">
          <el-select v-model="addform.groupId" placeholder="请选择" size="mini" @change="changegroupId">
            <el-option v-for="item in postproductLists" :key="item.groupId" :label="item.groupName"
              :value="item.groupId">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="开始时间:" prop="startDate">
          <el-date-picker :value-format="format" v-model="addform.startDate" placeholder="选择日期" size="mini">
          </el-date-picker>
        </el-form-item><br>
        <el-form-item label="结束时间:" prop="endDate">
          <el-date-picker :value-format="format" size="mini" v-model="addform.endDate" placeholder="选择日期">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="发团时间:" prop="numberTime">
          <el-date-picker :value-format="format" size="mini" v-model="addform.numberTime" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="返程时间:" prop="backTime">
          <el-date-picker :value-format="format" size="mini" v-model="addform.backTime" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="团号:" prop="httpServerAddress">
          <el-input size="mini" v-model="addform.a"></el-input>
        </el-form-item> -->
        <!-- <el-form-item label="价格:" prop="price">
          <el-input size="mini" v-model="addform.price"></el-input>
        </el-form-item> -->
        <el-form-item label="是否上架:" prop="numberStatus">
          <el-radio-group v-model="addform.numberStatus">
            <el-radio label="0">下架</el-radio>
            <el-radio label="1">上架</el-radio>
          </el-radio-group>
        </el-form-item><br />
        <el-form-item>
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="startOtherPlugin('addform')">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 查看团信息 -->
    <el-dialog :visible.sync="dialogFormview" width="50%" :close-on-click-modal="false">
      <div style="display:flex;">
        <div>
          <span>团名称：{{getGroupInfo.groupName}}</span>
        </div>
        <div style="margin:0 0 0 200px;">
          <span>团号：{{groupNumbera}}</span>
        </div>
      </div>
      <el-collapse v-model="activeNames">
        <el-collapse-item title="行程介绍" name="1">
          {{getGroupInfo.userTourItinerary}}
        </el-collapse-item>
        <el-collapse-item title="服务标准" name="2">
          {{getGroupInfo.userServiceStandard}}
        </el-collapse-item>
        <el-collapse-item title="费用不包含项目" name="3">
          {{getGroupInfo.userFeeNoContain}}
        </el-collapse-item>
        <el-collapse-item title="费用包含项目" name="4">
          {{getGroupInfo.userFeeContain}}
        </el-collapse-item>
        <el-collapse-item title="出行须知" name="5">
          {{getGroupInfo.travelNotes}}
        </el-collapse-item>
        <el-collapse-item title="购物点" name="6">
          {{getGroupInfo.shopPlace}}
        </el-collapse-item>
        <el-collapse-item title="自费景点介绍" name="7">
          {{getGroupInfo.ownFeePlaceInfo}}
        </el-collapse-item>
        <el-collapse-item title="退团机票预定" name="8">
          {{getGroupInfo.userFlightRefundRule}}
        </el-collapse-item>
        <el-collapse-item title="退团规则" name="9">
          {{getGroupInfo.groupRefundRule}}
        </el-collapse-item>
      </el-collapse>
      <el-button style="margin:20px 0 10px 40%" type="primary" @click="dialogFormview=false">取消</el-button>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'groupNumber',
    data() {
      return {
        groupNumbera: "",
        getGroupInfo: {},
        tractNumList: [],
        format: "yyyy-MM-dd",
        // 团产品查询条件
        resultsproductList: {
          groupType: "",
          groupStart: "",
          groupName: "",
          supplierName: "",
          supplierCount: "",
        },
        // 团产品下拉列表
        postproductLists: [],
        // 弹窗表头
        title: '',
        // 供应商列表
        travelSupplierVoList: [],
        // 查看团信息弹窗
        dialogFormview: false,
        activeNames: ['1'],
        // 团号id
        group: "",
        // 新增
        addform: {
          groupId: "", //团id	
          supplierId: "", //供应商id
          contractId: "", //合同id
          groupName: "", //旅游团名称	
          startDate: "", //开始时间
          endDate: "", //截止时间
          numberTime: "", //发团时间
          numberStatus: "0", //团号状态 0下架，1上架
          price: "", //价格
          backTime:""
        },
        // 表单验证
        rules: {
          supplierId: [{
            required: true,
            message: '请选择供应商',
            trigger: 'change'
          }],
          groupId: [{
            required: true,
            message: '请选择团名称',
            trigger: 'change'
          }],
          // startDate: [{
          //   required: true,
          //   message: '请选择日期',
          //   trigger: 'change'
          // }],
          // endDate: [{
          //   required: true,
          //   message: '请选择日期',
          //   trigger: 'change'
          // }],
          
          numberTime: [{
            required: true,
            message: '请选择日期',
            trigger: 'change'
          }],
           backTime: [{
            required: true,
            message: '请选择日期',
            trigger: 'change'
          }],
          // price: [{
          //   required: true,
          //   message: '请输入价格',
          //   trigger: 'blur'
          // }, ],
          resource: [{
            required: true,
            message: '请选择是否上架',
            trigger: 'change'
          }],

        },
        dialogFormVisible: false,
        pageTotal: 0,
        pageSize: 10,
        currentPage: 1,
        // 表格数据
        tableData: [],
        // 查询条件
        results: {
          countryType: '',
          groupStart: '',
          groupName: '',
          groupNumber: '',
        },
        // 团类型
        groupType: [{
            value: "",
            label: "全部"
          },
          {
            value: "domesticGroup",
            label: "国内团"
          },
          {
            value: "foreignMissions",
            label: "国外团"
          }
        ],
        // 团状态
        options: [{
            value: "",
            label: "全部"
          },
          // {
          //   value: 'wait',
          //   label: "等待上架"
          // },
          {
            value: "0",
            label: "下架"
          },
          {
            value: "1",
            label: "上架"
          }
        ], //暂时绑定下拉的数组

      }
    },
    methods: {
      changegroupId(row) {
        console.log(row)
      },
        supplierIdChange(){
      this.addform.contractId=""
      this.getcontractNum()
    },
      // 合同号列表
      getcontractNum() {
        this.$axios.getcontractNum({
            supplierId:this.addform.supplierId
        }).then((res) => {
          console.log(res)
          this.tractNumList = res.data
        })
      },
      // 团号列表
      getgroupNumList() {
        this.$axios
          .getgroupNumList({
            countryType: this.results.countryType,
            numberStatus: this.results.groupStart,
            groupName: this.results.groupName,
            groupNumber: this.results.groupNumber,
          })
          .then((res) => {
            this.tableData = res.data
            this.pageTotal = res.total
          });
      },
      // 供应商列表
      gettravelSupplierVoList() {
        localStorage.removeItem("pageNum");
        localStorage.removeItem("pageSize");
        this.$axios
          .gettravelSupplierVoList({})
          .then((res) => {
            localStorage.setItem("pageSize", 10);
            localStorage.setItem("pageNum", 1);
            console.log(res);
            if (res.code == "200") {
              this.travelSupplierVoList = res.data;

            }
          });
      },
      contractIdChange(){
         this.$forceUpdate()
        this.resultsproductList.supplierCount = this.addform.contractId
        // this.postproductList()
      },
      // 团产品列表
      postproductList() {
        localStorage.removeItem("pageNum");
        localStorage.removeItem("pageSize");
        this.$axios.postproductList(this.resultsproductList).then((res) => {
          console.log(res.data)
          localStorage.setItem("pageSize", 10);
          localStorage.setItem("pageNum", 1);
          this.postproductLists = res.data
        })
      },

      // 表格新增确定按钮
      startOtherPlugin(formName) {
        console.log(formName)
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.title == '新增') {
              this.getinsertGroupNum()
            } else {
              this.getupdateNum()
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      // 修改团号
      getupdateNum() {
        for (let i = 0; i < this.postproductLists.length; i++) {
          if (this.postproductLists[i].groupId == this.addform.groupId) {
            this.addform.groupName = this.postproductLists[i].groupName
          }
        }
        console.log(this.addform.groupName)
        localStorage.setItem("isToken", true);
        this.$axios.getupdateNum(this.addform).then((res) => {
          
          if (res.code == 200) {
            this.$message({
              message: '修改成功',
              type: 'success'
            });
            this.dialogFormVisible = false
            this.getgroupNumList()
          }
        })
      },

      // 新增团号  
      getinsertGroupNum() {
        for (let i = 0; i < this.postproductLists.length; i++) {
          if (this.postproductLists[i].groupId == this.addform.groupId) {
            this.addform.groupName = this.postproductLists[i].groupName
          }
        }
        console.log(this.addform)
        localStorage.setItem("isToken", true);
        this.$axios.getinsertGroupNum(this.addform).then((res) => {
          
          console.log(res)
          if (res.code == 200) {
            this.$message({
              message: '新增成功',
              type: 'success'
            });
            this.dialogFormVisible = false
            this.getgroupNumList()
          }
        })
      },
      // 新增按钮
      addproduct() {
        this.title = '新增'
        this.addform = {}
        this.dialogFormVisible = true
      },
      // 查询按钮
      viewproduct() {
        localStorage.setItem("pageNum", 1);
        localStorage.setItem("pageSize", 10);
        this.getgroupNumList()
      },
      // 表格修改按钮
      Mgroupodify(row) {
        this.title = '修改'
        this.addform = {}

        console.log(row)
        this.$axios.getselGroupNum({
          id: String(row.numberId)
        }).then((res) => {
          if (res.code == 200) {
            this.addform = res.data
            this.addform.backTime = res.data.backDate

          }
        })
        this.dialogFormVisible = true;
      },
      // 表格上架下架按钮
      shelves(row) {
        let type = ""
        if (row.numberStatusName=='待上架') {
          type = 1
        } else {
          type = 0
        }
        this.$axios.getoperate({
          id: row.numberId,
          type: type
        }).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: '操作成功',
              type: 'success'
            });
            this.getgroupNumList()
          }
        })
      },
      // 查看团消息按钮
      viewgroup(row) {
        this.groupNumbera = row.groupNumber
        console.log(row)
        this.$axios.gettravelProductDetails({
          groupId: row.groupId
        }).then((res) => {
          if (res.code == 200) {
            this.getGroupInfo = res.data
          }
        })
        this.dialogFormview = true;
      },
    //分页
    handleSizeChange(value) {
      console.log(1,value)
      localStorage.setItem("pageSize", value);
      localStorage.setItem("pageNum", 1);
      this.getgroupNumList();
      this.pageSize = value;
    },
    handleCurrentChange(value) {
      console.log(2,value,this.pageSize)

      localStorage.setItem("pageNum", value);
      localStorage.setItem("pageSize", this.pageSize);
      this.getgroupNumList();
    },
    search() {
      localStorage.setItem("pageNum", this.currentPage);
      localStorage.setItem("pageSize", 10);
      this.getgroupNumList();
    },
    },
    mounted() {
     localStorage.setItem("pageSize", 10);
            localStorage.setItem("pageNum", 1);
      this.viewproduct()
      this.gettravelSupplierVoList()
      this.postproductList()
      // // 合同号列表
      // this.getcontractNum()
    },
  }
</script>

<style scoped>
</style>